package com.hl.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.Free;
import com.hl.mapper.FreeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class FreeService {
    @Autowired
    private FreeMapper freeMapper;

    //查询所有公益信息
    public HashMap<String, Object> selectAllFree(int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        //查询全部数据
        List<Free> list = freeMapper.selectAllFree();
        PageInfo<Free> pageInfo = new PageInfo<Free>(list);
        //获取总数
        int total = (int) pageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }

    //分页条件查询
    public HashMap<String, Object> selectAllByPageAndCondition(String title, Date startDate, Date endDate, int page, int size){
        //设置分页
        PageHelper.startPage(page, size);
        //查询全部数据
        List<Free> list = freeMapper.selectAllByPageAndCondition(title,startDate,endDate);
        PageInfo<Free> pageInfo = new PageInfo<Free>(list);
        //获取总数
        int total = (int) pageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }

    //新增公益信息
    public int insertFree(Free free) {
        return freeMapper.insertFree(free);
    }

    //删除公益信息
    public Integer deleteByFreeId(Integer id) {
        return freeMapper.deleteByFreeId(id);
    }

    //修改公益信息
    public int updateFreeById(Free free){
        return freeMapper.updateFreeById(free);
    }

}
